<template>
  <!-- 加入购物车 -->
  <div class="bgc" @click.stop="changeStatus">
    <div class="addCar">
      <div class="addNum">
        <div class="reduce" @click.stop="decrease"><i></i></div>
        <input type="number" v-model.number="num" @blur="changeNum" @click.stop="" />
        <div class="plus" @click.stop="num += pack"><span></span><span></span></div>
      </div>
      <div class="sure" @click.stop="sureNum()">确定</div>
    </div>
  </div>
</template>

<script>
import { getCarts } from '@/api/activity/activity.js'

export default {
  props: {
    item: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      status: false,
      num: this.item.pack || 1,
      pack: this.item.pack
    }
  },
  methods: {
    changeStatus () {
      this.$emit('changStatus', false)
    },
    // 减法判断
    decrease () {
      if (this.num > this.pack) this.num -= this.pack
    },
    changeNum () {
      this.num % this.pack < this.pack
        ? (this.num = parseInt(this.num / this.pack) * this.pack)
        : ''
      this.num ? '' : (this.num = this.pack)
    },
    sureNum () {
      getCarts({ num: this.num, skuId: this.item.id })
        .then(res => {
          if (res.code === 200) {
            uni.showToast({ icon: 'none', title: '成功', mask: true })
          } else {
            uni.showToast({ icon: 'none', title: res.msg, mask: true })
          }
        })
        .finally(() => { this.changeStatus() })
    }
  }
}
</script>

<style lang="scss" scoped>
.bgc {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 999;

  .activityRules {
    width: 100%;
    height: 462rpx;
    position: fixed;
    bottom: 0;
    right: 0;
    background: #ffffff;

    .rulesTitle {
      position: relative;
      height: 97.22rpx;
      line-height: 97.22rpx;
      text-align: center;
      border-bottom: 1px solid #f9f9f9;

      h2 {
        font-size: 33rpx;
        font-weight: 500;
        color: #000000;
      }

      ::v-deep .uni-icons {
        position: absolute;
        top: 0;
        right: 47.92rpx;
      }
    }

    .limits {
      padding: 58.33rpx 25rpx 0;
      display: flex;
      height: 32rpx;

      .reduction {
        display: inline-block;
        border: 1px solid #ed1b19;
        border-radius: 4rpx;
        font-size: 18rpx;
        font-weight: 400;
        color: #ed1b19;
        line-height: 1;
        padding: 4rpx 8rpx;
        margin-right: 17.36rpx;
      }

      .reductionNum {
        font-size: 25rpx;
        font-weight: 400;
        color: #8d8d8d;

        div {
          margin-bottom: 16rpx;
        }
      }
    }
  }

  .addCar {
    position: fixed;
    top: 50%;
    left: 50%;
    background: #ffffff;
    border-radius: 11rpx;
    z-index: 1000;
    width: 500rpx;
    height: 302rpx;
    transform: translate(-50%, -50%);

    .addNum {
      display: flex;
      justify-content: space-between;
      margin: 69.44rpx;
      margin-bottom: 47.22rpx;

      .reduce {
        display: inline-block;
        width: 58rpx;
        height: 58rpx;
        border: 1px solid #2871fc;
        border-radius: 8rpx;
        position: relative;

        i {
          display: inline-block;
          width: 31rpx;
          height: 1rpx;
          background: #2871fc;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
    }

    input {
      display: block;
      width: 201rpx;
      height: 58rpx;
      border: 1px solid #2871fc;
      border-radius: 8rpx;
      color: #ef231b;
      text-align: center;
    }

    .plus {
      display: inline-block;
      width: 58rpx;
      height: 58rpx;
      border: 1px solid #2871fc;
      border-radius: 8rpx;
      position: relative;

      span:nth-child(1) {
        display: inline-block;
        width: 31rpx;
        height: 1rpx;
        background: #2871fc;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }

      span:nth-child(2) {
        display: inline-block;
        width: 1rpx;
        height: 31rpx;
        background: #2871fc;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }

    .sure {
      width: 326rpx;
      height: 58rpx;
      line-height: 58rpx;
      background: #2871fc;
      border-radius: 29rpx;
      font-size: 29rpx;
      font-weight: 400;
      color: #ffffff;
      text-align: center;

      position: absolute;
      left: 50%;
      transform: translateX(-50%);
    }
  }
}
</style>